package com.six666.medicaldevicetestingsystem.controller;

import com.six666.medicaldevicetestingsystem.dto.Login_DTO;
import com.six666.medicaldevicetestingsystem.entity.BaseResponse;
import com.six666.medicaldevicetestingsystem.entity.UserAccount;
import com.six666.medicaldevicetestingsystem.service.Account_Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
@CrossOrigin
@RestController
public class Account_Controller {
    @Autowired
    private Account_Service account_service;
    @PostMapping("/login_msg")
    public BaseResponse<Login_DTO> login(@RequestParam Map<String,String> params) {
        String username = params.get("username");
        String password = params.get("password");
        if (username==null||password==null) {
            return new BaseResponse<>(101,null,"参数错误","必填值为空");
        }
        else {
            Login_DTO data=new Login_DTO(account_service.login(username, password));
            return new BaseResponse<>(data);
        }
    }
    @PostMapping("/sign_msg")
    public BaseResponse<Login_DTO> sign(@RequestParam Map<String,String> params) {
        String username = params.get("username");
        String password = params.get("password");
        String name = params.get("name");
        String contact = params.get("contact");
        String institution = params.get("institution");

        String account_type=params.get("account_type");
        if (account_type==null) {
            account_type="Experimenters";
        }
        if (username==null||password==null||name==null||contact==null||institution==null) {
            return new BaseResponse<>(101,null,"参数错误","必填值为空");
        }
        else {
            if (!account_type.equals("Experimenters") && !account_type.equals("Supportstaff")) {
                return new BaseResponse<>(103,null,"参数错误","account_type输入有误");
            }
        }
        UserAccount user=new UserAccount();
        user.setUsername(username);
        user.setPassword(password);
        user.setName(name);
        user.setContact(contact);
        user.setInstitution(institution);
        user.setAccounttype(account_type);
        Login_DTO data=new Login_DTO(account_service.sign(user));
        return new BaseResponse<>(data);
    }

}
